package config;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;

/**
 * @author CHENTAIWEN
 * @create 2022/8/3 15:54
 */

@Component
@Aspect
@Slf4j
public class apiLog {

    private static final Logger logger = LoggerFactory.getLogger(apiLog.class);

    @Pointcut(value = "")
    public void log() {
    }
/*
    //    含参切面方法
    @Around("")
    public Object controllerLog(ProceedingJoinPoint pdj) throws Throwable {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

//        传参
        Object[] args = pdj.getArgs();//获取参数数组
        Object ret = pdj.proceed(args);//执行原方法并获取返回结果

        // 记录下请求内容
        StringBuilder sb = new StringBuilder();
        sb.append("请求URL: " + request.getRequestURL().toString());
        sb.append("   接口路径: " + pdj.getSignature().getDeclaringTypeName() + "." + pdj.getSignature().getName());
        sb.append("   Params: " + Arrays.toString(args));
//        接口路径
        logger.error(sb.toString());
        log.error("响应报文: {}", ret);
        return ret;
    }*/
}
